package com.scau.yangsuiyu.estore.dababases.dao;

import com.scau.yangsuiyu.estore.dababases.entity.Shop;
import com.scau.yangsuiyu.estore.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * @author suiyu_yang
 * @description Shop的Dao类
 * @date 2019-06-08 15:45
 * @email suiyu_yang@163.com
 */
public class ShopDao {

    public void insert(Shop shop) {
        Session session = HibernateUtil.currentSession();
        Transaction transaction = session.beginTransaction();

        session.save(shop);

        transaction.commit();
        HibernateUtil.closeSession();
    }

    public void delete(Shop shop) {
        Session session = HibernateUtil.currentSession();
        Transaction transaction = session.beginTransaction();

        session.delete(shop);

        transaction.commit();
        HibernateUtil.closeSession();
    }

    public void update(Shop shop) {
        Session session = HibernateUtil.currentSession();
        Transaction transaction = session.beginTransaction();

        session.update(shop);

        session.flush();
        transaction.commit();
        HibernateUtil.closeSession();
    }

    public Shop getShopByName(String shopName) {
        Session session = HibernateUtil.currentSession();

        String sql = "from Shop shop where shop.shopName = '" + shopName + "' ";

        Shop shop = (Shop) session.createQuery(sql).uniqueResult();

        HibernateUtil.closeSession();
        return shop;
    }

    public Shop getShopById(int id) {
        Session session = HibernateUtil.currentSession();

        String sql = "from Shop shop where shop.id = '" + id + "' ";

        Shop shop = (Shop) session.createQuery(sql).uniqueResult();

        HibernateUtil.closeSession();
        return shop;
    }
}
